package org.epanetgrid.otimizacao.gui;

import static org.epanetgrid.otimizacao.tui.ArgOptions.FO;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import org.epanetgrid.otimizacao.OtimizadorBuilder;
import org.epanetgrid.otimizacao.tui.ArgOptions;
import org.epanetgrid.otimizacao.tui.OtimizadorMain;
import org.epanetgrid.otimizacao.tui.StatusOtimizacao;

public class GUIMain {

	protected JFrame mainFrame = null;  //  @jve:decl-index=0:visual-constraint="161,10"
	protected JPanel mainPanel = null;
	protected JPanel otimizacaoPanel = null;
	protected JPanel opcoesGeraisPanel = null;
	protected JPanel algGenPanel = null;
	protected JPanel funcoesObjetivoPanel = null;
	protected JCheckBox custoPorVolumeCheckBox = null;
	protected JCheckBox numeroChaveamentosCheckBox;
	protected JCheckBox variacaoDeNivelCheckBox;
	protected JPanel parametrosPanel = null;
	protected JPanel metodosSelecaoPanel = null;
	protected JPanel resultadosPanel;
	protected JSpinner representativasSpinner;
	protected JCheckBox minimaPonderadaCheckBox;
	protected JSpinner custoPorVolumePonderado;
	protected JSpinner numeroDeChaveamentosSpinner;
	protected JSpinner variacaoDeNivelSpinner;
	protected JSpinner tamanhoDaPopulacaoSpinner;
	protected JSpinner tamanhoDaPopulacaoExternaSpinner;
	protected JSpinner porcElitismoSpinner;
	protected JSpinner porcCrossoverNormalSpinner;
	protected JSpinner probCrossoverNormalSpinner;
	protected JSpinner probCrossoverDirecionadoSpinner;
	protected JSpinner probMutacaoNormalSpinner;
	protected JSpinner porcBombasMutadasSpinner;
	protected JSpinner amostragemEstocasticaSpinner;
	protected JCheckBox gradeCheckBox;
	protected JSpinner gradeSpinner;
	protected JSpinner tempoMaximoSpinner;
	protected JCheckBox tempoMaximoCheckBox;
	protected JSpinner numeroDeGeracoesSpinner;
	protected JCheckBox numeroDeGeracoesCheckBox;
	protected JCheckBox planilhaCheckBox;
	protected JTextField planilhaTextField;
	protected JButton planilhaSalvarButton;
	protected JCheckBox controleManualCheckBox;
	protected JTextField controleManualTextField;
	protected JButton controleManualButton;
	protected JCheckBox lerPopulacaoInicialCheckBox;
	protected JTextField lerPopulacaoInicialTextField;
	protected JButton lerPopulacaoInicialButton;
	protected JCheckBox csvCheckBox;
	protected JCheckBox salvarLogCheckBox;
	protected JButton salvarLogButton;
	protected JTextField salvarLogTextField;
	protected JPanel okCancelPanel;
	protected JButton okButton;
	protected JButton canceButton;
	private JRadioButton amostragemEstocasticaButton;
	private JRadioButton torneioButton;
	private JPanel filesPanel;
	private JTextField networkFileTextField;
	private JButton networkFileButton;
	private JTextField operacaoFileTextField;
	private JButton operacaoFileButton;

	public JFrame getMainFrame() {
		if (mainFrame == null) {
			mainFrame = new JFrame();
	        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			mainFrame.setSize(new Dimension(700, 550));
			mainFrame.getContentPane().add(getMainPanel(), BorderLayout.CENTER);
			mainFrame.setTitle("EpanetGrid - AG");
		}
		return mainFrame;
	}

	protected JPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new JPanel();
			Box vbox = Box.createVerticalBox();
			Box hbox = Box.createHorizontalBox();
			hbox.add(getOtimizacaoPanel());
			hbox.add(getAlgGenPanel());
			vbox.add(hbox);
			vbox.add(getOpcoesGeraisPanel());
			vbox.add(getFilesPanel());
			vbox.add(getOkCancelPanel());
			mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
			mainPanel.add(vbox);
		}
		return mainPanel;
	}

	// <FILES>
	
	protected JPanel getFilesPanel() {
		if (filesPanel == null) {
			filesPanel = new JPanel();
			filesPanel.setLayout(new BoxLayout(filesPanel, BoxLayout.PAGE_AXIS));
			Box vbox = Box.createVerticalBox();
			Box hbox = Box.createHorizontalBox();
			hbox.add(getNetworkFileTextField());
			hbox.add(getNetworkFileButton());
			vbox.add(hbox);
			hbox = Box.createHorizontalBox();
			hbox.add(getOperacaoFileTextField());
			hbox.add(getOperacaoFileButton());
			vbox.add(hbox);
			filesPanel.add(vbox);
		}
		return filesPanel;
	}
	
	// <OTIMIZAÇÃO>
	
	protected JPanel getOtimizacaoPanel() {
		if (otimizacaoPanel == null) {
			otimizacaoPanel = new JPanel();
			otimizacaoPanel.setLayout(new BoxLayout(otimizacaoPanel, BoxLayout.PAGE_AXIS));
			Box vbox = Box.createVerticalBox();
			vbox.add(getFuncoesObjetivoPanel());
			vbox.add(getResultadosPanel());
			otimizacaoPanel.add(vbox);
		}
		return otimizacaoPanel;
	}
	
	protected JTextField getNetworkFileTextField() {
		if (networkFileTextField == null) {
			networkFileTextField = new JTextField();
		}
		return networkFileTextField;
	}
	
	protected JButton getNetworkFileButton() {
		if (networkFileButton == null) {
			networkFileButton = new JButton("Abrir...");
		}
		return networkFileButton;
	}

	protected JTextField getOperacaoFileTextField() {
		if (operacaoFileTextField == null) {
			operacaoFileTextField = new JTextField();
		}
		return operacaoFileTextField;
	}
	
	protected JButton getOperacaoFileButton() {
		if (operacaoFileButton == null) {
			operacaoFileButton = new JButton("Abrir...");
		}
		return operacaoFileButton;
	}
	
	// 		<FUNÇÔES OBJETIVAS>
	
	protected JPanel getFuncoesObjetivoPanel() {
		if (funcoesObjetivoPanel == null) {
			funcoesObjetivoPanel = new JPanel();
			funcoesObjetivoPanel.setLayout(new BoxLayout(funcoesObjetivoPanel, BoxLayout.PAGE_AXIS));
			funcoesObjetivoPanel.setBorder(BorderFactory.createTitledBorder(null, "Funções objetivo", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			Box hbox = Box.createHorizontalBox();
			Box vbox = Box.createVerticalBox();
			vbox.add(getCustoPorVolumeCheckBox());
			vbox.add(getNumeroChaveamentosCheckBox());
			vbox.add(getVariacaoDeNivelCheckBox());
			hbox.add(vbox);
			hbox.add(Box.createHorizontalGlue());
			funcoesObjetivoPanel.add(hbox);
		}
		return funcoesObjetivoPanel;
	}

	protected JCheckBox getCustoPorVolumeCheckBox() {
		if (custoPorVolumeCheckBox == null) {
			custoPorVolumeCheckBox = new JCheckBox();
			custoPorVolumeCheckBox.setText("Custo por volume ponderado");
		}
		return custoPorVolumeCheckBox;
	}
	
	protected JCheckBox getNumeroChaveamentosCheckBox() {
		if (numeroChaveamentosCheckBox == null) {
			numeroChaveamentosCheckBox = new JCheckBox();
			numeroChaveamentosCheckBox.setText("Número de chaveamentos");
		}
		return numeroChaveamentosCheckBox;
	}
	
	protected JCheckBox getVariacaoDeNivelCheckBox() {
		if (variacaoDeNivelCheckBox == null) {
			variacaoDeNivelCheckBox = new JCheckBox();
			variacaoDeNivelCheckBox.setText("Variação de nível");
		}
		return variacaoDeNivelCheckBox;
	}

	// 		</FUNÇÔES OBJETIVAS>

	// 		<RESULTADOS>
	
	protected JPanel getResultadosPanel() {
		if (resultadosPanel == null) {
			resultadosPanel = new JPanel();
						
			resultadosPanel.setLayout(new BoxLayout(resultadosPanel, BoxLayout.PAGE_AXIS));
			resultadosPanel.setBorder(BorderFactory.createTitledBorder(null, "Resultados", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			
			Box vbox = Box.createVerticalBox();
			
			Box hbox = Box.createHorizontalBox();

			JLabel representativasLabel = new JLabel("Quant. de soluções representativas");
			hbox.add(representativasLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(Box.createRigidArea(new Dimension(5,0)));
			hbox.add(getRepresentativasSpinner());
			vbox.add(hbox);
			vbox.add(Box.createRigidArea(new Dimension(0,10)));
			
			hbox = Box.createHorizontalBox();
			hbox.add(getMinimaPonderadaCheckBox());
			hbox.add(Box.createHorizontalGlue());
			vbox.add(hbox);
			vbox.add(Box.createVerticalGlue());

			hbox = Box.createHorizontalBox();
			
			JLabel custoPorVolumePonderadoLabel = new JLabel("Custo por volume ponderado");
			hbox.add(custoPorVolumePonderadoLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getCustoPorVolumePonderadoSpinner());
			vbox.add(hbox);
			
			hbox = Box.createHorizontalBox();
			
			JLabel numeroDeChaveamentosLabel = new JLabel("Número de chaveamentos");
			hbox.add(numeroDeChaveamentosLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getNumeroDeChaveamentosSpinner());
			vbox.add(hbox);
			
			hbox = Box.createHorizontalBox();
			
			JLabel variacaoDeNivelLabel = new JLabel("Variação de nível");
			hbox.add(variacaoDeNivelLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getVariacaoDeNivelSpinner());
			vbox.add(hbox);
			
			resultadosPanel.add(vbox);
		}
		return resultadosPanel;
	}
	
	protected JSpinner getRepresentativasSpinner() {
		if (representativasSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(5, 0, Integer.MAX_VALUE, 1);
			representativasSpinner = new JSpinner(model);
			((JSpinner.DefaultEditor)representativasSpinner.getEditor()).getTextField().setColumns(2);
		}
		return representativasSpinner;
	}
	
	protected JCheckBox getMinimaPonderadaCheckBox() {
		if (minimaPonderadaCheckBox == null) {
			minimaPonderadaCheckBox = new JCheckBox();
			minimaPonderadaCheckBox.setText("Mínima ponderada");
		}
		return minimaPonderadaCheckBox;
	}
	
	protected JSpinner getCustoPorVolumePonderadoSpinner() {
		if (custoPorVolumePonderado == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(33, 0, 100, 1); 
			custoPorVolumePonderado = new JSpinner(model);
			//((JSpinner.DefaultEditor)custoPorVolumePonderado.getEditor()).getTextField().setColumns(2);
		}
		return custoPorVolumePonderado;
	}
	
	protected JSpinner getNumeroDeChaveamentosSpinner() {
		if (numeroDeChaveamentosSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(33, 0, 100, 1); 
			numeroDeChaveamentosSpinner = new JSpinner(model);
		}
		return numeroDeChaveamentosSpinner;
	}
	
	protected JSpinner getVariacaoDeNivelSpinner() {
		if (variacaoDeNivelSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(34, 0, 100, 1);
			variacaoDeNivelSpinner = new JSpinner(model);
		}
		return variacaoDeNivelSpinner;
	}

	// 		</RESULTADOS>
	// </OTIMIZAÇÃO>

	// <ALG GEN>
	protected JPanel getAlgGenPanel() {
		if (algGenPanel == null) {
			algGenPanel = new JPanel();
			algGenPanel.setBorder(BorderFactory.createTitledBorder(null, "Configurações do algoritmo genético", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			algGenPanel.setLayout(new BoxLayout(algGenPanel, BoxLayout.PAGE_AXIS));
			
			Box vbox = Box.createVerticalBox();
			vbox.add(getParametrosPanel());
			vbox.add(getMetodosSelecaoPanel());
			algGenPanel.add(vbox);
		}
		return algGenPanel;
	}
	
	protected JPanel getParametrosPanel() {
		if (parametrosPanel == null) {
			parametrosPanel = new JPanel();
			parametrosPanel.setLayout(new BoxLayout(parametrosPanel, BoxLayout.PAGE_AXIS));
			parametrosPanel.setBorder(BorderFactory.createTitledBorder(null, "Parâmetros", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			
			Box vbox = Box.createVerticalBox();
			
			Box hbox = Box.createHorizontalBox();
			
			JLabel tamanhoDaPopulacaoLabel = new JLabel("Tamanho da população");
			hbox.add(tamanhoDaPopulacaoLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getTamanhoDaPopulacaoSpinner());
			vbox.add(hbox);
			
			hbox = Box.createHorizontalBox();
			JLabel tamanhoDaPopulacaoExternaLabel = new JLabel("Tamanho da população externa");
			hbox.add(tamanhoDaPopulacaoExternaLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getTamanhoDaPopulacaoExternaSpinner());
			vbox.add(hbox);
				
			hbox = Box.createHorizontalBox();
			JLabel porcElitissmoLabel = new JLabel("Porc. de elitismo");
			hbox.add(porcElitissmoLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getPorcElitismoSpinner());
			vbox.add(hbox);
			
			hbox = Box.createHorizontalBox();
			JLabel porcCrossoverNormalLabel = new JLabel("Porc. de crossover normal");
			hbox.add(porcCrossoverNormalLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getPorcCrossoverNormalSpinner());
			vbox.add(hbox);
			
			hbox = Box.createHorizontalBox();
			JLabel probCrossoverNormalLabel = new JLabel("Prob. de crossover normal");
			hbox.add(probCrossoverNormalLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getProbCrossoverNormalSpinner());
			vbox.add(hbox);
			
			hbox = Box.createHorizontalBox();
			JLabel probCrossoverDirecionadoLabel = new JLabel("Prob. de crossover direcionado");
			hbox.add(probCrossoverDirecionadoLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getProbCrossoverDirecionadoSpinner());
			vbox.add(hbox);
			
			hbox = Box.createHorizontalBox();
			JLabel probMutacaoNormalLabel = new JLabel("Prob. de mutação normal (intervalo)");
			hbox.add(probMutacaoNormalLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getProbMutacaoNormalSpinner());
			vbox.add(hbox);
			
			hbox = Box.createHorizontalBox();
			JLabel porcBombasMutadasLabel = new JLabel("Porc. de bombas mutadas");
			hbox.add(porcBombasMutadasLabel);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getPorcBombasMutadasSpinner());
			vbox.add(hbox);
			
			parametrosPanel.add(vbox);
			
		}
		return parametrosPanel;
	}

	protected JSpinner getTamanhoDaPopulacaoSpinner() {
		if (tamanhoDaPopulacaoSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(50, 0, Integer.MAX_VALUE, 1); 
			tamanhoDaPopulacaoSpinner = new JSpinner(model);
		}
		return tamanhoDaPopulacaoSpinner;
	}
	
	protected JSpinner getTamanhoDaPopulacaoExternaSpinner() {
		if (tamanhoDaPopulacaoExternaSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(1, 0, 1, 0.01); 
			tamanhoDaPopulacaoExternaSpinner = new JSpinner(model);
		}
		return tamanhoDaPopulacaoExternaSpinner;
	}
	
	protected JSpinner getPorcElitismoSpinner() {
		if (porcElitismoSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(1, 0, 1, 0.01); 
			porcElitismoSpinner = new JSpinner(model);
		}
		return porcElitismoSpinner;
	}
	
	protected JSpinner getPorcCrossoverNormalSpinner() {
		if (porcCrossoverNormalSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(1, 0, 1, 0.01); 
			porcCrossoverNormalSpinner = new JSpinner(model);
		}
		return porcCrossoverNormalSpinner;
	}
	
	protected JSpinner getProbCrossoverNormalSpinner() {
		if (probCrossoverNormalSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(1, 0, 1, 0.01); 
			probCrossoverNormalSpinner = new JSpinner(model);
		}
		return probCrossoverNormalSpinner;
	}
	
	protected JSpinner getProbCrossoverDirecionadoSpinner() {
		if (probCrossoverDirecionadoSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(1, 0, 1, 0.01); 
			probCrossoverDirecionadoSpinner = new JSpinner(model);
		}
		return probCrossoverDirecionadoSpinner;
	}
	
	protected JSpinner getProbMutacaoNormalSpinner() {
		if (probMutacaoNormalSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(1, 0, 1, 0.01); 
			probMutacaoNormalSpinner = new JSpinner(model);
		}
		return probMutacaoNormalSpinner;
	}
	
	protected JSpinner getPorcBombasMutadasSpinner() {
		if (porcBombasMutadasSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(1, 0, 1, 0.01); 
			porcBombasMutadasSpinner = new JSpinner(model);
		}
		return porcBombasMutadasSpinner;
	}
	
	
	protected JPanel getMetodosSelecaoPanel() {
		if (metodosSelecaoPanel == null) {
			metodosSelecaoPanel = new JPanel();
			metodosSelecaoPanel.setLayout(new BoxLayout(metodosSelecaoPanel, BoxLayout.PAGE_AXIS));
			metodosSelecaoPanel.setBorder(BorderFactory.createTitledBorder(null, "Métodos de seleção", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			
			Box vbox = Box.createVerticalBox();
			Box hbox = Box.createHorizontalBox();
			
			ButtonGroup metodosSelecaoGroup = new ButtonGroup();
			amostragemEstocasticaButton = new JRadioButton("Amostragem estocástica universal - S.U.S. (% da seleção)");
			hbox.add(amostragemEstocasticaButton);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getAmostragemEstocasticaSpinner());
			vbox.add(hbox);
			vbox.add(Box.createVerticalGlue());
			
			hbox = Box.createHorizontalBox();
			torneioButton = new JRadioButton("Torneio");
			hbox.add(torneioButton);
			hbox.add(Box.createHorizontalGlue());
			vbox.add(hbox);
			
			metodosSelecaoGroup.add(amostragemEstocasticaButton);
			metodosSelecaoGroup.add(torneioButton);
			metodosSelecaoPanel.add(vbox);
			
		}
		return metodosSelecaoPanel;
	}
	
	protected JSpinner getAmostragemEstocasticaSpinner() {
		if (amostragemEstocasticaSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(1, 0, 1, 0.01); 
			amostragemEstocasticaSpinner = new JSpinner(model);
		}
		return amostragemEstocasticaSpinner;
	}

	// </ALG GEN>
	
	// <OPÇÕES GERAIS>
	
	protected JPanel getOpcoesGeraisPanel() {
		if (opcoesGeraisPanel == null) {
			opcoesGeraisPanel = new JPanel();
			opcoesGeraisPanel.setLayout(new BoxLayout(opcoesGeraisPanel, BoxLayout.PAGE_AXIS));
			opcoesGeraisPanel.setBorder(BorderFactory.createTitledBorder(null, "Opções gerais", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			
			Box hboxMain = Box.createHorizontalBox(); 
			
			Box vbox = Box.createVerticalBox();
			Box hbox = Box.createHorizontalBox();
			
			hbox.add(getGradeCheckBox());
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getGradeSpinner());
			vbox.add(hbox);
			
			hbox = Box.createHorizontalBox();
			hbox.add(getNumeroDeGeracoesCheckBox());
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getNumeroDeGeracoesSpinner());
			vbox.add(hbox);
			
			hbox = Box.createHorizontalBox();
			hbox.add(getTempoMaximoCheckBox());
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getTempoMaximoSpinner());
			vbox.add(hbox);

			hbox = Box.createHorizontalBox();
			hbox.add(getPlanilhaCheckBox());
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getPlanilhaTextField());
			hbox.add(Box.createHorizontalGlue());
			hbox.add(getPlanilhaSalvarButton());
			vbox.add(hbox);			
			
			hboxMain.add(vbox);
			
			vbox = Box.createVerticalBox();
			
			hbox = Box.createHorizontalBox();
			hbox.add(getControleManualCheckBox());
			hbox.add(getControleManualTextField());
			hbox.add(getControleManualButton());
			vbox.add(hbox);
			
			hbox = Box.createHorizontalBox();
			hbox.add(getLerPopulacaoInicialCheckBox());
			hbox.add(getLerPopulacaoInicialTextField());
			hbox.add(getLerPopulacaoInicialButton());
			vbox.add(hbox);
			
			vbox.add(getCSVCheckBox());
			
			hbox = Box.createHorizontalBox();
			hbox.add(getSalvarLogCheckBox());
			hbox.add(getSalvarLogTextField());
			hbox.add(getSalvarLogButton());
			vbox.add(hbox);
			hboxMain.add(vbox);
			
			opcoesGeraisPanel.add(hboxMain);
		}
		return opcoesGeraisPanel;
	}

	protected JCheckBox getNumeroDeGeracoesCheckBox() {
		if (numeroDeGeracoesCheckBox == null) {
			numeroDeGeracoesCheckBox = new JCheckBox();
			numeroDeGeracoesCheckBox.setText("Usar a grade computacional (Indivíduos/Máquina)");
		}
		return numeroDeGeracoesCheckBox;
	}

	protected JSpinner getNumeroDeGeracoesSpinner() {
		if (numeroDeGeracoesSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(1, 0, 1, 0.01); 
			numeroDeGeracoesSpinner = new JSpinner(model);
		}
		return numeroDeGeracoesSpinner;
	}
	
	
	protected JCheckBox getGradeCheckBox() {
		if (gradeCheckBox == null) {
			gradeCheckBox = new JCheckBox();
			gradeCheckBox.setText("Número de gerações");
		}
		return gradeCheckBox;
	}

	protected JSpinner getGradeSpinner() {
		if (gradeSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(1, 0, 1, 0.01); 
			gradeSpinner = new JSpinner(model);
		}
		return gradeSpinner;
	}
	
	protected JCheckBox getTempoMaximoCheckBox() {
		if (tempoMaximoCheckBox == null) {
			tempoMaximoCheckBox = new JCheckBox();
			tempoMaximoCheckBox.setText("Tempo máximo (minutos)");
		}
		return tempoMaximoCheckBox;
	}
	
	protected JSpinner getTempoMaximoSpinner() {
		if (tempoMaximoSpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(1, 0, 1, 0.01); 
			tempoMaximoSpinner = new JSpinner(model);
		}
		return tempoMaximoSpinner;
	}
	
	protected JCheckBox getPlanilhaCheckBox() {
		if (planilhaCheckBox == null) {
			planilhaCheckBox = new JCheckBox();
			planilhaCheckBox.setText("Salvar planilha");
		}
		return planilhaCheckBox;
	}
	
	protected JTextField getPlanilhaTextField() {
		if (planilhaTextField == null) {
			planilhaTextField = new JTextField();
		}
		return planilhaTextField;
	}
	
	protected JButton getPlanilhaSalvarButton() {
		if (planilhaSalvarButton == null) {
			planilhaSalvarButton = new JButton("Salvar...");
		}
		return planilhaSalvarButton;
	}

	protected JCheckBox getControleManualCheckBox() {
		if (controleManualCheckBox == null) {
			controleManualCheckBox = new JCheckBox();
			controleManualCheckBox.setText("Usar controle manual");
		}
		return controleManualCheckBox;
	}
	
	protected JTextField getControleManualTextField() {
		if (controleManualTextField == null) {
			controleManualTextField = new JTextField();
		}
		return controleManualTextField;
	}
	
	protected JButton getControleManualButton() {
		if (controleManualButton == null) {
			controleManualButton = new JButton("Salvar...");
		}
		return controleManualButton;
	}

	protected JCheckBox getLerPopulacaoInicialCheckBox() {
		if (lerPopulacaoInicialCheckBox == null) {
			lerPopulacaoInicialCheckBox = new JCheckBox();
			lerPopulacaoInicialCheckBox.setText("Ler população inicial");
		}
		return controleManualCheckBox;
	}
	
	protected JTextField getLerPopulacaoInicialTextField() {
		if (lerPopulacaoInicialTextField == null) {
			lerPopulacaoInicialTextField = new JTextField();
		}
		return lerPopulacaoInicialTextField;
	}
	
	protected JButton getLerPopulacaoInicialButton() {
		if (lerPopulacaoInicialButton == null) {
			lerPopulacaoInicialButton = new JButton("Salvar...");
		}
		return lerPopulacaoInicialButton;
	}

	protected JCheckBox getCSVCheckBox() {
		if (csvCheckBox == null) {
			csvCheckBox = new JCheckBox();
			csvCheckBox.setText("Usar calculador de solução viável");
		}
		return csvCheckBox;
	}
	
	protected JCheckBox getSalvarLogCheckBox() {
		if (salvarLogCheckBox == null) {
			salvarLogCheckBox = new JCheckBox();
			salvarLogCheckBox.setText("Ler população inicial");
		}
		return salvarLogCheckBox;
	}
	
	protected JTextField getSalvarLogTextField() {
		if (salvarLogTextField == null) {
			salvarLogTextField = new JTextField();
		}
		return salvarLogTextField;
	}
	
	protected JButton getSalvarLogButton() {
		if (salvarLogButton == null) {
			salvarLogButton = new JButton("Salvar...");
		}
		return salvarLogButton;
	}
	
	// </OPÇÕES GERAIS>

	// <OK_CANCEL>
	
	protected JPanel getOkCancelPanel() {
		if (okCancelPanel == null) {
			okCancelPanel = new JPanel();
			okCancelPanel.setLayout(new BoxLayout(okCancelPanel,BoxLayout.LINE_AXIS));
			Box hbox = Box.createHorizontalBox();
			hbox.add(getOkButton());
			hbox.add(getCancelButton());
			okCancelPanel.add(hbox);
		}
		return okCancelPanel;
	}
	
	protected JButton getCancelButton() {
		if (canceButton == null) {
			canceButton = new JButton("Cancel");
		}
		return canceButton;
	}

	protected JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton("Ok");
			okButton.addActionListener(new ActionListener() {
				ArgOptions argOptions = new ArgOptions();
				@Override
				public void actionPerformed(ActionEvent e) {
					if ("Ok".equals(e.getActionCommand())) {
						List<String> fos = new LinkedList<String>();
						if (numeroChaveamentosCheckBox.isSelected()) {
							fos.add(ArgOptions.FO_CHAVEAMENTOS);
						}
						if (custoPorVolumeCheckBox.isSelected()) {
							fos.add(ArgOptions.FO_CUSTO_TOTAL);
						}
						if (variacaoDeNivelCheckBox.isSelected()) {
							fos.add(ArgOptions.FO_NIVEIS);
						}
						if (fos.isEmpty()) {
							// TODO EMPTY!
							System.out.println("CANNOT BE EMPTY!");
							return;
						}

						String[] fos2 = new String[fos.size()]; 

						for (int i = 0; i < fos2.length; i++) {
							fos2[i] = fos.get(i);
						}
						
						argOptions.setOption(ArgOptions.FO, fos2);
						
						argOptions.setOption(ArgOptions.REPRESENTATIVAS, representativasSpinner.getValue().toString());

						// TODO MINIMA PONDRADA!!!

						argOptions.setOption(ArgOptions.TAM_POP_INICIAL, tamanhoDaPopulacaoSpinner.getValue().toString());
						argOptions.setOption(ArgOptions.POP_EXT, tamanhoDaPopulacaoExternaSpinner.getValue().toString());
						argOptions.setOption(ArgOptions.ELITISMO, porcElitismoSpinner.getValue().toString());
						argOptions.setOption(ArgOptions.PORC_XO_NORMAL, porcCrossoverNormalSpinner.getValue().toString());
						argOptions.setOption(ArgOptions.PROB_XO_NORMAL, probCrossoverNormalSpinner.getValue().toString());
						// TODO PROB_XO DIRECIONADA
						argOptions.setOption(ArgOptions.PROB_MUT_NORMAL_INTERVALO, probMutacaoNormalSpinner.getValue().toString());
						argOptions.setOption(ArgOptions.BOMBAS_MUTADAS, porcBombasMutadasSpinner.getValue().toString());
						
						if (amostragemEstocasticaButton.isSelected()) {
							argOptions.setOption(ArgOptions.SELECAO, ArgOptions.SELECAO_SUS);
							// TODO USAR SUS DA SELECAO
						} else {
							argOptions.setOption(ArgOptions.SELECAO, ArgOptions.SELECAO_TORNEIO);
						}
						
						if (numeroDeGeracoesCheckBox.isSelected()) {
							argOptions.setOption(ArgOptions.MAX_GERACOES, numeroDeGeracoesSpinner.getValue().toString());
						}
						
						if (gradeCheckBox.isSelected()) {
							argOptions.setOption(ArgOptions.GRID, gradeSpinner.getValue().toString());
						}

						if (tempoMaximoCheckBox.isSelected()) {
							argOptions.setOption(ArgOptions.TEMPO_MAX, tempoMaximoSpinner.getValue().toString());
						}

						if (planilhaCheckBox.isSelected()) {
							argOptions.setOption(ArgOptions.PLANILHA_XLS, planilhaTextField.getText());
						}
						
						if (planilhaCheckBox.isSelected()) {
							argOptions.setOption(ArgOptions.PLANILHA_XLS, planilhaTextField.getText());
						}
						
						if (salvarLogCheckBox.isSelected()) {
							argOptions.setOption(ArgOptions.LOG, salvarLogTextField.getText());
						}
						
						if (controleManualCheckBox.isSelected()) {
							// TODO O QUE FAZER AQUI????
						}
						
						if (csvCheckBox.isSelected()) {
							// TODO USAR CSV!!!
						}
						
						if (lerPopulacaoInicialCheckBox.isSelected()) {
							argOptions.setOption(ArgOptions.CONTROLE_INICIAL, lerPopulacaoInicialButton.getText());
						}

						argOptions.setNetworkFile(networkFileTextField.getText());
						
						argOptions.setOperacaoFile(operacaoFileTextField.getText());
						
						OtimizadorMain otimizador = new OtimizadorMain();
						
						StatusOtimizacao statusOtimizacao = new StatusOtimizacao(argOptions.getOptionValues(FO).length);
						
						otimizador.start(argOptions, statusOtimizacao);
						
						System.out.println("OK!");
						
					}
				}
				
			});
		}
		return okButton;
	}
	
	// </OK_CANCEL>

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				GUIMain window = new GUIMain();
				window.getMainFrame().setVisible(true);
			}
		});
	}
	
}
